package com.smile.frame.seckill.core.disruptor;

import com.lmax.disruptor.EventTranslatorTwoArg;
import com.lmax.disruptor.RingBuffer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 生产者
 *
 * @author LGC
 */
@Service
@RequiredArgsConstructor
public class SecondKillEventProducer {

    private final RingBuffer<SecondKillEvent> secondKillEventRingBuffer;

    private final static EventTranslatorTwoArg<SecondKillEvent, Long, Long> translator = (event, sequence, arg0, arg1) -> {
        event.setSeckillId(arg0);
        event.setUserId(arg1);
    };

    public void producer(SecondKillEvent event) {
        // 获取ringBuffer环，用于接取生产者生产的事件
        secondKillEventRingBuffer.publishEvent(translator, event.getSeckillId(), event.getUserId());
    }
}